package com.vtb.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vtb.base.VtbBaseServlet;
import com.vtb.entity.DataCache;
import com.vtb.entity.QqDetailInfo;
import com.vtb.entity.QqOauth;
import com.vtb.entity.RrDetailInfo;
import com.vtb.entity.RrOauth;
import com.vtb.entity.SsOauth;
import com.vtb.entity.Var;
import com.vtb.utils.DateUtils;

@SuppressWarnings("serial")
public class OpenAndClose extends VtbBaseServlet {
 
    private static final Log log = LogFactory.getLog(OpenAndClose.class);
    protected RrOauth rrOauth;
    protected QqOauth qqOauth;
    protected SsOauth ssOauth;
    protected RrDetailInfo rrDetailInfo;
    protected QqDetailInfo qqDetailInfo;
    protected String status;
    protected String a2b;
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request, response);
        if (lastMap == null || lastMap.size() != 2) {
            response.sendRedirect("error?err_code=1003");
            return;
        }
        rrOauth = (RrOauth) lastMap.get("rr");
        qqOauth = (QqOauth) lastMap.get("qq");
        ssOauth = (SsOauth) lastMap.get("ss");
        rrDetailInfo = (RrDetailInfo) lastMap.get("rr");
        qqDetailInfo = (QqDetailInfo) lastMap.get("qq");
        status = request.getParameter("status");
        a2b = request.getParameter("a2b");
    }

    /**
     * 关闭并删除信息
     *
     * @param t
     * @param k
     * @return
     * @throws IOException
     * @author Chen Tao
     * @date 2012-7-5 下午12:09:51
     */
    protected<T, K> boolean closeTb(T t,K k) throws IOException {
        if (t.getClass().isInstance(RrOauth.class) && k.getClass().isInstance(QqOauth.class)) {
            try {
                qqDao.del(qqOauth);
                rrDao.del(rrOauth);
                DataCache.synRemMap(qqOauth);
                DataCache.synRemMap(rrOauth);
                DataCache.mcount.decrementAndGet();
            } catch (Exception e) {
                log.error("关闭同步错误：", e);
                return false;
            }
            session.setAttribute("r2q", null);
            session.setAttribute("q2r", null);
        }
       if (t.getClass().isInstance(RrOauth.class) && k.getClass().isInstance(SsOauth.class)) {
           try {
               rrDao.del(rrOauth);
               ssDao.del(ssOauth);
               DataCache.synRemMap(rrOauth);
               DataCache.synRemMap(ssOauth);
               DataCache.mcount.decrementAndGet();
           } catch (Exception e) {
               log.error("关闭同步错误：", e);
               return false;
           }
           session.setAttribute("r2s", null);
           session.setAttribute("s2r", null);
       }
       if (t.getClass().isInstance(QqOauth.class) && k.getClass().isInstance(SsOauth.class)) {
           try {
               qqDao.del(qqOauth);
               ssDao.del(ssOauth);
               DataCache.synRemMap(qqOauth);
               DataCache.synRemMap(ssOauth);
               DataCache.mcount.decrementAndGet();
           } catch (Exception e) {
               log.error("关闭同步错误：", e);
               return false;
           }
           session.setAttribute("q2s", null);
           session.setAttribute("s2q", null);
       }
       if (lastMap.size()==2) {
           lastMap = null;
           session.setAttribute("status", "closed");
       }
       return true;
    }

    /**
     * 首次开启人人和腾讯
     *
     * @param rrOauth
     * @param qqOauth
     * @param a2b
     * @return
     * @throws IOException
     * @author Chen Tao
     * @date 2012-7-5 上午11:41:53
     */
    protected boolean openRqTb(RrOauth rrOauth, QqOauth qqOauth ,String a2b) throws IOException {
        rrDetailInfo.setRefq(qqOauth.getUni());
        qqDetailInfo.setRefr(qqOauth.getUni());
        if ("r2q".equals(a2b)) {
            rrDetailInfo.setR2q(Var.OPEN);
            qqDetailInfo.setQ2r(Var.CLOSE);
        }
        if ("q2r".equals(a2b)) {
            rrDetailInfo.setR2q(Var.CLOSE);
            qqDetailInfo.setQ2r(Var.OPEN);
        }
        rrDetailInfo.setR2s(Var.CLOSE);
        qqDetailInfo.setQ2s(Var.CLOSE);
        // 在map中加入oauth 插入数据库
        try {
            qqDao.insert(qqDetailInfo);
            rrDao.insert(rrDetailInfo);
            DataCache.rrlastTime.putIfAbsent(rrOauth.getUni(), DateUtils.createTime());
            DataCache.qqlastTime.putIfAbsent(qqOauth.getUni(), DateUtils.systemTime());
            DataCache.synPutMap(rrOauth);
            DataCache.synPutMap(qqOauth);
            DataCache.increment();
            if ("r2q".equals(a2b)) {
                session.setAttribute("r2q", "close");
            }
            if ("q2r".equals(a2b)) {
                session.setAttribute("q2r", "close");
            }
        } catch (Exception e) {
            // 失败后还原数据
            rrDetailInfo.setRefq(null);
            qqDetailInfo.setRefr(null);
            rrDetailInfo.setR2q(null);
            qqDetailInfo.setQ2r(null);
            log.error("开启人人和腾讯同步错误：", e);
            return false;
        }
        return true;
    }
    
    /**
     * 首次开启人人和新浪
     *
     * @param rrOauth
     * @param ssOauth
     * @param a2b
     * @return
     * @throws IOException
     * @author Chen Tao
     * @date 2012-7-5 上午11:43:07
     */
    protected boolean openRsTb(RrOauth rrOauth, SsOauth ssOauth ,String a2b) throws IOException {
        rrDetailInfo.setRefs(ssOauth.getUni());
        ssOauth.setRefr(rrOauth.getUni());
        if ("r2s".equals(a2b)) {
            rrDetailInfo.setR2s(Var.OPEN);
            ssOauth.setS2r(Var.CLOSE);
        }
        if ("s2r".equals(a2b)) {
            rrDetailInfo.setR2s(Var.CLOSE);
            ssOauth.setS2r(Var.OPEN);
        }
        rrDetailInfo.setR2q(Var.CLOSE);
        ssOauth.setS2q(Var.CLOSE);
        // 在map中加入oauth 插入数据库
        try {
            rrDao.insert(rrDetailInfo);
            ssDao.insert(ssOauth);
            DataCache.rrlastTime.putIfAbsent(rrOauth.getUni(), DateUtils.createTime());
            //DataCache.qqlastTime.putIfAbsent(qqOauth.getUni(), DateUtils.systemTime());
            DataCache.synPutMap(rrOauth);
            DataCache.synPutMap(ssOauth);
            DataCache.increment();
            if ("r2s".equals(a2b)) {
                session.setAttribute("r2s", "close");
            }
            if ("s2r".equals(a2b)) {
                session.setAttribute("s2r", "close");
            }
        } catch (Exception e) {
            // 失败后还原数据
            rrDetailInfo.setRefs(null);
            ssOauth.setRefr(null);
            rrDetailInfo.setR2s(null);
            ssOauth.setS2r(null);
            log.error("开启人人和新浪同步错误：", e);
            return false;
        }
        return true;
    }
    
    /**
     * 首次开启新浪腾讯
     *
     * @param qqOauth
     * @param ssOauth
     * @param a2b
     * @return
     * @throws IOException
     * @author Chen Tao
     * @date 2012-7-5 上午11:48:23
     */
    protected boolean openQsTb(QqOauth qqOauth, SsOauth ssOauth ,String a2b) throws IOException {
        qqDetailInfo.setRefs(ssOauth.getUni());
        ssOauth.setRefq(qqOauth.getUni());
        if ("q2s".equals(a2b)) {
            qqDetailInfo.setQ2s(Var.OPEN);
            ssOauth.setS2q(Var.CLOSE);
        }
        if ("s2q".equals(a2b)) {
            qqDetailInfo.setQ2s(Var.CLOSE);
            ssOauth.setS2q(Var.OPEN);
        }
        qqDetailInfo.setQ2r(Var.CLOSE);
        ssOauth.setS2r(Var.CLOSE);
        // 在map中加入oauth 插入数据库
        try {
            qqDao.insert(qqDetailInfo);
            ssDao.insert(ssOauth);
            //DataCache.sslastTime.putIfAbsent(rrOauth.getUni(), DateUtils.createTime());
            DataCache.qqlastTime.putIfAbsent(qqOauth.getUni(), DateUtils.systemTime());
            
            DataCache.synPutMap(qqOauth);
            DataCache.synPutMap(ssOauth);
            DataCache.increment();
            if ("q2s".equals(a2b)) {
                session.setAttribute("q2s", "close");
            }
            if ("q2r".equals(a2b)) {
                session.setAttribute("q2r", "close");
            }
        } catch (Exception e) {
            // 失败后还原数据
            qqDetailInfo.setRefs(null);
            ssOauth.setRefq(null);
            qqDetailInfo.setQ2s(null);
            ssOauth.setS2q(null);
            log.error("开启新浪腾讯同步错误：", e);
            return false;
        }
        return true;
    }
}
